﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Status;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;

namespace Repeatr
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }

        SystemState phoneMissedCalls = new SystemState(SystemProperty.PhoneMissedCalls);
        SystemState messagingSmsUnread = new SystemState(SystemProperty.MessagingSmsUnread);
        SystemState messagingTotalEmailUnread = new SystemState(SystemProperty.MessagingTotalEmailUnread);
        SystemState messagingVoiceMailTotalUnread = new SystemState(SystemProperty.MessagingVoiceMailTotalUnread);

        private void frmMain_Load(object sender, EventArgs e)
        {
            phoneMissedCalls.Changed += new ChangeEventHandler(phoneMissedCalls_Changed);
            messagingSmsUnread.Changed += new ChangeEventHandler(messagingSmsUnread_Changed);
            messagingTotalEmailUnread.Changed += new ChangeEventHandler(messagingTotalEmailUnread_Changed);
            messagingVoiceMailTotalUnread.Changed += new ChangeEventHandler(messagingVoiceMailTotalUnread_Changed);
        }

        bool missedPhoneCallAlert = false;
        void phoneMissedCalls_Changed(object sender, ChangeEventArgs args)
        {
            if (checkBoxMissedCallAlert.Checked == false)
                return;

            missedPhoneCallAlert = true;
            timer.Enabled = true;
        }

        bool unreadMessagingAlert = false;
        void messagingSmsUnread_Changed(object sender, ChangeEventArgs args)
        {
            if (checkBoxUnreadSms.Checked == false)
                return;

            unreadMessagingAlert = true;
            timer.Enabled = true;
        }

        bool unreadEmailAlert = false;
        void messagingTotalEmailUnread_Changed(object sender, ChangeEventArgs args)
        {
            if (checkBoxUnreadEmailAlert.Checked == false)
                return;

            unreadEmailAlert = true;
            timer.Enabled = true;
        }

        bool unreadVoiceMailAlert = false;
        void messagingVoiceMailTotalUnread_Changed(object sender, ChangeEventArgs args)
        {
            if (checkBoxUnreadVoiceMailAlert.Checked == false)
                return;

            unreadVoiceMailAlert = true;
            timer.Enabled = true;
        }

        private void menuItemClearAlerts_Click(object sender, EventArgs e)
        {
            missedPhoneCallAlert = false;
            unreadMessagingAlert = false;
            unreadEmailAlert = false;
            unreadVoiceMailAlert = false;

            timer.Enabled = false;

            menuItemClearAlerts.Enabled = false;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (missedPhoneCallAlert == true ||
                unreadMessagingAlert == true ||
                unreadEmailAlert == true ||
                unreadVoiceMailAlert == true)
            {
                PlaySound();

                menuItemClearAlerts.Enabled = true;
            }
        }

        [DllImport("aygshell.dll")]
        static extern uint SndPlaySync(string pszSoundFile, uint dwFlags);

        void PlaySound()
        {
            string path;
            path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

            SndPlaySync(Path.Combine(path, "alert.wma"), 0);
        }

        private void numericUpDownRepeatInterval_ValueChanged(object sender, EventArgs e)
        {
            timer.Interval = 60 * 1000 * (int)numericUpDownRepeatInterval.Value;
        }
    }
}